package longrise.phone.com.bjjt_henan.compensate;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILFMsgListener;
import com.longrise.android.LFView;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.MainActivity;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;
import longrise.phone.com.bjjt_henan.widget.SelectDialog;

/**
 * 保险报案后，温馨提示，选择线上，还是线下定损，Created by Administrator on 2016/5/12.
 */

public class IntentInsuranceReportView extends LFView implements View.OnClickListener, ILFMsgListener {
    private String singlecar;
    private Context context = null;
    private View view = null;
    private EntityBean[] CreateAgreementbean;
    private String appCaseno;
    private LinearLayout hn_btn_back;
    private TextView tv_title;
    private BaseApplication application;
    private EntityBean cpBeans;//保险公司集合
    private Dialog cpsDialog;
    private LinearLayout onlineLayout;
    private LinearLayout dealLayout;
    private int type = 0;

    private boolean isFinish = false;//理赔中心跳转进入

    public IntentInsuranceReportView(Context c, EntityBean[] CreateAgreementbean, String appCaseno) {
        super(c);
        this.context = c;
        this.CreateAgreementbean = CreateAgreementbean;
        this.appCaseno = appCaseno;
    }

    public IntentInsuranceReportView(Context c, EntityBean[] CreateAgreementbean, String appCaseno, String singlecar) {
        super(c);
        this.context = c;
        this.CreateAgreementbean = CreateAgreementbean;
        this.appCaseno = appCaseno;
        this.singlecar = singlecar;
    }

    public void setTypes(int type) {
        this.type = type;
    }

    public void setFinish(boolean finish) {
        isFinish = finish;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
        addILFMsgListener(this);
        ((MainActivity) context).initHelpView(view, appCaseno);
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_intentinsurancereport, null);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("请选择您的情形");
        onlineLayout = (LinearLayout) view.findViewById(R.id.insurancereport_layout_online);
        dealLayout = (LinearLayout) view.findViewById(R.id.insurancereport_layout_deal);

        application = ((MainActivity) context).getAppData();

        showFlash();
    }

    //显示动画效果
    private void showFlash() {
        TextView dot = (TextView) view.findViewById(R.id.tv_flash);
        //缩放动画
        Animation scale = new ScaleAnimation(1.0f, 0.8f, 1.0f, 0.8f, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scale.setDuration(800);
        scale.setRepeatMode(Animation.REVERSE);
        scale.setRepeatCount(Animation.INFINITE);
        //渐变动画
        AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.7f);
        alpha.setDuration(800);
        scale.setRepeatMode(Animation.REVERSE);
        alpha.setRepeatCount(Animation.INFINITE);

        AnimationSet set = new AnimationSet(true);
        set.addAnimation(scale);
        set.addAnimation(alpha);

        dot.startAnimation(set);
    }

    private void regEvent(boolean b) {
        if (null != onlineLayout) {
            onlineLayout.setOnClickListener(b ? this : null);
        }
        if (null != dealLayout) {
            dealLayout.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.insurancereport_layout_online://网上定损理赔
                reportInsurance(type);
                break;
            case R.id.insurancereport_layout_deal://已报案快速处理
                if (CreateAgreementbean != null && CreateAgreementbean.length > 0) {
                    String duty = CreateAgreementbean[0].getString("dutytype");
                    if (TextUtils.equals(duty, "2")) {//同责默认为交强险
                        reportInsurance(1);
                    } else {//其他情况选择保险公司
                        showSelectDialog();
                    }
                }
                break;
            case R.id.hn_btn_back:
                OnDestroy();
                closeForm();
                break;
            default:
                break;
        }
    }

    private SelectDialog selectDialog;

    private void showSelectDialog() {
        if (null == selectDialog) {
            selectDialog = new SelectDialog(context, getData());
            selectDialog.setTitle("请选择保险类型");
            selectDialog.setOnSelectListener(new SelectDialog.onSelectListener() {
                @Override
                public void onCancel() {

                }

                @Override
                public void onSelect(int select) {
                    if (select < 0) {
                        UiUtil.showToast(context, "请先选择保险类型");
                        return;
                    }
                    selectDialog.dismiss();
                    saveCpsType(String.valueOf(select + 1));
                }
            });
        }
        selectDialog.show();
    }

    private List<String> getData() {
        List<String> list = new ArrayList<>();
        list.add("交强险");
        list.add("商业险");

        return list;
    }

    private void saveCpsType(String type) {
        String userflag = ((MainActivity) context).getAppData().getKckpName();
        String token = ((MainActivity) context).getAppData().getToken();
        DialogUtils.showDialog(context);
        ApiImpI.getInstance(context).saveCpstype(appCaseno, userflag, token, type, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                DialogUtils.closeDialog();
            }

            @Override
            public void onSuccess(Object result) {
                DialogUtils.closeDialog();
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                if (TextUtils.equals(restate, "1")) {
                    reportInsurance(1);
                } else {
                    String redes = bean.getString("redes");
                    UiUtil.showToast(context, redes);
                }
            }
        });
    }

    //进入保险报案
    private void reportInsurance(int flag) {
        if (null == cpBeans) {//未获取到保险公司集合
            getCplist(flag);
            return;
        }
        InsuranceReportView insurancereportview = null;
        if (TextUtils.equals("0", singlecar)) {
            insurancereportview = new InsuranceReportView(context, CreateAgreementbean, appCaseno, "0");
        } else {
            insurancereportview = new InsuranceReportView(context, CreateAgreementbean, appCaseno);
        }
        insurancereportview.setCpBeans(cpBeans);
        insurancereportview.setFlag(flag);
        FrameworkManager.getInstance().showNewForm(context, insurancereportview);
    }

    //获取报案车牌号
    private String getCaseCarno() {
        String casecarno = null;
        if (TextUtils.equals("0", singlecar)) {//单车
            casecarno = CreateAgreementbean[0].getString("casecarno");
        } else {
            for (int i = 0; i < CreateAgreementbean.length; i++) {
                String ownother = CreateAgreementbean[i].getString("party");
                if (TextUtils.equals(ownother, "1")) {
                    casecarno = CreateAgreementbean[i].getString("casecarno");
                }
            }
        }
        return casecarno;
    }

    //加载保险公司
    private void getCplist(final int flag) {
        String userflag = application.getKckpName();
        String token = application.getToken();
        if (null == cpsDialog) {
            cpsDialog = UiUtil.showProcessDialog(context, "查询承保信息中");
        } else {
            cpsDialog.show();
        }
        ApiImpI.getInstance(context).searchCplist(userflag, token, getCaseCarno(), appCaseno,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        if (null != cpsDialog && cpsDialog.isShowing()) {
                            cpsDialog.dismiss();
                        }
                        if (!TextUtils.isEmpty(error)) {
                            UiUtil.showToast(context, error);
                        }
                    }

                    @Override
                    public void onSuccess(Object result) {
                        if (null != cpsDialog && cpsDialog.isShowing()) {
                            cpsDialog.dismiss();
                        }
                        EntityBean bean = (EntityBean) result;
                        String restate = bean.getString("restate");
                        if (TextUtils.equals(restate, "1")) {
                            cpBeans = bean.getBean("data");
                            if (null != cpBeans) {
                                reportInsurance(flag);
                                return;
                            }
                        }
                        String redes = bean.getString("redes");
                        if (!TextUtils.isEmpty(redes)) {
                            UiUtil.showToast(context, redes);
                        }
                    }
                });
    }

    @Override
    public Object onLFMsg(int i, Object... objects) {
        if (i == -10 && isFinish) {//点击返回按键
            ((MainActivity) context).backToMainView();
        }
        return null;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        regEvent(false);
        isFinish = false;
        removeILFMsgListener(this);
        CreateAgreementbean = null;
        view = null;
        if (null != selectDialog) {
            selectDialog.dismiss();
            selectDialog = null;
        }
        if (null != cpsDialog) {
            cpsDialog.dismiss();
            cpsDialog = null;
        }
    }
}
